using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.Design;
using AjaxControlToolkit;
using Telerik.Web.UI;

namespace Acms.WebControls
{
    public class RadGridSequenceColumn : GridColumn
    {
        public RadGridSequenceColumn()
        {
            this.ButtonType = Telerik.Web.UI.ButtonType.LinkButton;
        }

        public override GridColumn Clone()
        {
            RadGridSequenceColumn tmp = new RadGridSequenceColumn();
            return tmp;
        }

        public override void InitializeCell(TableCell cell, int columnIndex, GridItem inItem)
        {
            if (inItem is GridDataItem && !inItem.IsInEditMode)
            {
                if (ShowSequence && !string.IsNullOrEmpty(SequenceField))
                {
                    Label lblSequence = new Label();
                    lblSequence.ID = "litSequence";
                    lblSequence.DataBinding += new EventHandler(lblSequence_DataBinding);
                    cell.Controls.Add(lblSequence);
                }
                //if sort expression is natural or by sequence field
                if (this.Owner.SortExpressions.Count == 0 || this.Owner.SortExpressions[0].FieldName == this.SequenceField)
                {
                    Control btnUp = null;
                    Control btnDown = null;
                    if (inItem.DataSetIndex != 0)
                    {
                        btnUp = CreateUpControl(inItem.ItemIndex);
                    }
                    if (inItem.DataSetIndex + 1 != this.Owner.DataSourceCount)
                    {
                        btnDown = CreateDownControl(inItem.ItemIndex);
                    }
                    if (btnUp != null)
                    {
                        btnUp.ID = "btnUp";
                        cell.Controls.Add(btnUp);
                    }
                    if (btnUp != null && btnDown != null)
                    {
                        cell.Controls.Add(new LiteralControl(" "));
                    }
                    if (btnDown != null)
                    {
                        btnDown.ID = "btnDown";
                        cell.Controls.Add(btnDown);
                    }
                }
            }
            else
            {
                base.InitializeCell(cell, columnIndex, inItem);
            }
        }

        void lblSequence_DataBinding(object sender, EventArgs e)
        {
            Label tmp = sender as Label;
            if (tmp != null)
            {
                object dataItem = DataBinder.GetDataItem(tmp.NamingContainer);
                object val = DataBinder.Eval(dataItem, SequenceField);
                if (val != null)
                {
                    tmp.Text = val.ToString();
                }
            }
        }

        private Control CreateUpControl(int commandArgument)
        {
            switch (ButtonType)
            {
                case Telerik.Web.UI.ButtonType.PushButton:
                    Button tmpPush = new Button();
                    tmpPush.Text = GetUpText();
                    tmpPush.ToolTip = GetUpText();
                    tmpPush.CommandName = "MoveUp";
                    tmpPush.CssClass = "up";
                    tmpPush.CommandArgument = commandArgument.ToString();
                    return tmpPush;
                case Telerik.Web.UI.ButtonType.LinkButton:
                    LinkButton tmpLink = new LinkButton();
                    tmpLink.Text = GetUpText();
                    tmpLink.ToolTip = GetUpText();
                    tmpLink.CommandName = "MoveUp";
                    tmpLink.CssClass = "up";
                    tmpLink.CommandArgument = commandArgument.ToString();
                    return tmpLink;
                case Telerik.Web.UI.ButtonType.ImageButton:
                    ImageButton tmpImage = new ImageButton();
                    tmpImage.ImageUrl = UpIconUrl;
                    tmpImage.ToolTip = GetUpText();
                    tmpImage.CommandName = "MoveUp";
                    tmpImage.CssClass = "up";
                    tmpImage.CommandArgument = commandArgument.ToString();
                    return tmpImage;
                default:
                    return null;
            }
        }

        private Control CreateDownControl(int commandArgument)
        {
            switch (ButtonType)
            {
                case Telerik.Web.UI.ButtonType.PushButton:
                    Button tmpPush = new Button();
                    tmpPush.Text = GetDownText();
                    tmpPush.ToolTip = GetDownText();
                    tmpPush.CommandName = "MoveDown";
                    tmpPush.CssClass = "down";
                    tmpPush.CommandArgument = commandArgument.ToString();
                    return tmpPush;
                case Telerik.Web.UI.ButtonType.LinkButton:
                    LinkButton tmpLink = new LinkButton();
                    tmpLink.Text = GetDownText();
                    tmpLink.ToolTip = GetDownText();
                    tmpLink.CommandName = "MoveDown";
                    tmpLink.CssClass = "down";
                    tmpLink.CommandArgument = commandArgument.ToString();
                    return tmpLink;
                case Telerik.Web.UI.ButtonType.ImageButton:
                    ImageButton tmpImage = new ImageButton();
                    tmpImage.ImageUrl = DownIconUrl;
                    tmpImage.ToolTip = GetDownText();
                    tmpImage.CssClass = "down";
                    tmpImage.CommandName = "MoveDown";
                    tmpImage.CommandArgument = commandArgument.ToString();
                    return tmpImage;
                default:
                    return null;
            }
        }

        private string GetUpText()
        {
            return string.IsNullOrEmpty(UpText) ? "Move Up" : UpText;
        }

        private string GetDownText()
        {
            return string.IsNullOrEmpty(DownText) ? "Move Down" : DownText;
        }

        public Telerik.Web.UI.ButtonType ButtonType { get; set; }
        public string SequenceField { get; set; }
        public string UpText { get; set; }
        public string DownText { get; set; }
        public string UpIconUrl { get; set; }
        public string DownIconUrl { get; set; }

        public bool ShowSequence { get; set; }
    }
}
